﻿<CodeSnippetTabbed DocLink="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxScheduler.VisibleResourcesDataSource">
<CodeSnippetTabPage Text="Razor">@(@"<div class=""demo-resnavigator"">
    <div class=""demo-resnavigator-container card"">
        <div class=""demo-resnavigator-title card-body"">
            <DxButton RenderStyle=""ButtonRenderStyle.None"" @onclick=""@ToggleResourceNavigator"">
                <span class=""demo-resnavigator-icon""></span>
            </DxButton>
            @if(ResourceNavigatorExpanded) {
                <span style="""">Resources</span>
            }
        </div>
        @if(ResourceNavigatorExpanded) {
            <div class=""demo-resnavigator-tree card-body"">
                    <DxTreeView @ref=""@TreeView""
                                Data=""@ResourceAppointmentCollection.GetResourceGroups()""
                                TextExpression=""@(dataItem => ((Resource)dataItem).Name)""
                                ChildrenExpression=""@(dataItem => GetNodeChildren(dataItem))"">
                        <NodeTextTemplate>
                            @{
                                Resource resource = (Resource)context.DataItem;
                                if(resource.IsGroup) {
                                    <span>@resource.Name</span>
                                }
                                else {
                                    <DxCheckBox T=""bool""
                                                Checked=""@GetIsVisible(resource)""
                                                CheckedChanged=""@(visible => UpdateResourceVisibilty(resource, visible))""
                                                style=""margin-left: -28px"">
                                        @resource.Name
                                    </DxCheckBox>
                                }
                            }
                        </NodeTextTemplate>
                    </DxTreeView>
            </div>
        }
    </div>
    <div class=""demo-resnavigator-sc-container"">
        <DxScheduler StartDate=""@DateTime.Today""
                     DataStorage=""@DataStorage""
                     @bind-VisibleResourcesDataSource=""@VisibleResourcesSource""
                     GroupType=""SchedulerGroupType.Resource""
                     ResourceNavigatorVisible=""false""
                     CssClass=""demo-sc-docked mw-1100"">
            <DxSchedulerDayView DayCount=""1"" ShowWorkTimeOnly=""true""></DxSchedulerDayView>
        </DxScheduler>
    </div>
</div>

@code {
    DxSchedulerDataStorage DataStorage = new DxSchedulerDataStorage() {
        AppointmentsSource = ResourceAppointmentCollection.GetAppointments(),
        AppointmentMappings = new DxSchedulerAppointmentMappings() {
            Type = ""AppointmentType"",
            Start = ""StartDate"",
            End = ""EndDate"",
            Subject = ""Caption"",
            AllDay = ""AllDay"",
            Location = ""Location"",
            Description = ""Description"",
            LabelId = ""Label"",
            StatusId = ""Status"",
            RecurrenceInfo = ""Recurrence"",
            ResourceId = ""ResourceId""
        },
        ResourcesSource = ResourceAppointmentCollection.GetResources(),
        ResourceMappings = new DxSchedulerResourceMappings() {
            Id = ""Id"",
            Caption = ""Name"",
            BackgroundCssClass = ""BackgroundCss"",
            TextCssClass = ""TextCss""
        }
    };

    List<Resource> VisibleResources = ResourceAppointmentCollection.GetResources().Take(2).ToList();

    IEnumerable VisibleResourcesSource {
        get => VisibleResources;
        set => VisibleResources = value.Cast<Resource>().ToList();
    }

    bool ResourceNavigatorExpanded { get; set; } = true;

    DxTreeView TreeView;

    bool GetIsVisible(Resource resource) {
        return VisibleResources.Contains(resource);
    }

    void UpdateResourceVisibilty(Resource resource, bool visible) {
        if(visible)
            VisibleResources.Add(resource);
        else
            VisibleResources.Remove(resource);
        VisibleResources = VisibleResources.OrderBy(p => p.Id).ToList();
    }

    IEnumerable GetNodeChildren(object dataItem) {
        Resource resource = (Resource)dataItem;
        if(resource.IsGroup) {
            return ResourceAppointmentCollection.GetResources()
                .Where(childResource => childResource.GroupId == resource.Id);
        }
        return null;
    }

    void ToggleResourceNavigator() {
        ResourceNavigatorExpanded = !ResourceNavigatorExpanded;
    }

    protected override void OnAfterRender(bool firstRender) {
        TreeView.ExpandAll();
    }
}
")
</CodeSnippetTabPage>
<CodeSnippetTabPage Text="ResourceAppointment">@(@"public class ResourceAppointment {
    public ResourceAppointment() { }

    public int AppointmentType { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Caption { get; set; }
    public string Description { get; set; }
    public string Location { get; set; }
    public int? Label { get; set; }
    public int Status { get; set; }
    public bool AllDay { get; set; }
    public string Recurrence { get; set; }
    public int? ResourceId { get; set; }
}")
</CodeSnippetTabPage>
<CodeSnippetTabPage Text="Resource">@(@"public class Resource {
    public int Id { get; set; }
    public int? GroupId { get; set; }
    public string Name { get; set; }
    public bool IsGroup { get; set; }

    public string TextCss { get; set; }
    public string BackgroundCss { get; set; }

    public override bool Equals(object obj) {
        Resource resource = obj as Resource;
        return resource != null && resource.Id == Id;
    }

    public override int GetHashCode() {
        return Id;
    }
}")
</CodeSnippetTabPage>
<CodeSnippetTabPage Text="ResourceAppointmentCollection">@(@"public static partial class ResourceAppointmentCollection {
    public static List<ResourceAppointment> GetAppointments() {
        DateTime date = DateTime.Now.Date;
        var dataSource = new List<ResourceAppointment>() {
            new ResourceAppointment {
                Caption = ""Install New Router in Dev Room"",
                StartDate = date + (new TimeSpan(0, 10, 0, 0)),
                EndDate = date + (new TimeSpan(0, 12, 0, 0)),
                Status = 1,
                ResourceId = 0
            },
            new ResourceAppointment {
                Caption = ""Upgrade Personal Computers"",
                StartDate = date + (new TimeSpan(0,  13, 0, 0)),
                EndDate = date + (new TimeSpan(0, 14, 30, 0)),
                Status = 1,
                ResourceId = 0
            },
            new ResourceAppointment {
                Caption = ""Website Re-Design Plan"",
                StartDate = date + (new TimeSpan(1, 9, 30, 0)),
                EndDate = date + (new TimeSpan(1, 11, 30, 0)),
                Status = 1,
                ResourceId = 0
            },
            new ResourceAppointment {
                Caption = ""New Brochures"",
                StartDate = date + (new TimeSpan(1, 13, 30, 0)),
                EndDate = date + (new TimeSpan(1, 15, 15, 0)),
                Status = 1,
                ResourceId = 0
            },
            new ResourceAppointment {
                Caption = ""Book Flights to San Fran for Sales Trip"",
                StartDate = date + (new TimeSpan(1, 12, 0, 0)),
                EndDate = date + (new TimeSpan(1, 13, 0, 0)),
                AllDay = true,
                Status = 1,
                ResourceId = 0
            },
            new ResourceAppointment {
                Caption = ""Approve Personal Computer Upgrade Plan"",
                StartDate = date + (new TimeSpan(0, 10, 0, 0)),
                EndDate = date + (new TimeSpan(0, 12, 0, 0)),
                Status = 1
            },
            new ResourceAppointment {
                Caption = ""Final Budget Review"",
                StartDate = date + (new TimeSpan(0, 13, 0, 0)),
                EndDate = date + (new TimeSpan(0, 15, 0, 0)),
                Status = 1,
                ResourceId = 1
            },
            new ResourceAppointment {
                Caption = ""Install New Database"",
                StartDate = date + (new TimeSpan(0, 9, 45, 0)),
                EndDate = date + (new TimeSpan(1, 11, 15, 0)),
                Status = 1,
                ResourceId = 1
            },
            new ResourceAppointment {
                Caption = ""Approve New Online Marketing Strategy"",
                StartDate = date + (new TimeSpan(1,  12, 0, 0)),
                EndDate = date + (new TimeSpan(1, 14, 0, 0)),
                Status = 1,
                ResourceId = 1
            },
            new ResourceAppointment {
                Caption = ""Customer Workshop"",
                StartDate = date + (new TimeSpan(0,  11, 0, 0)),
                EndDate = date + (new TimeSpan(0, 12, 0, 0)),
                AllDay = true,
                Status = 1,
                ResourceId = 2
            },
            new ResourceAppointment {
                Caption = ""Prepare 2021 Marketing Plan"",
                StartDate = date + (new TimeSpan(0,  11, 0, 0)),
                EndDate = date + (new TimeSpan(0, 13, 30, 0)),
                Status = 1,
                ResourceId = 2
            },
            new ResourceAppointment {
                Caption = ""Brochure Design Review"",
                StartDate = date + (new TimeSpan(0, 14, 0, 0)),
                EndDate = date + (new TimeSpan(0, 15, 30, 0)),
                Status = 1,
                ResourceId = 2
            },
            new ResourceAppointment {
                Caption = ""Create Icons for Website"",
                StartDate = date + (new TimeSpan(1, 10, 0, 0)),
                EndDate = date + (new TimeSpan(1, 11, 30, 0)),
                Status = 1,
                ResourceId = 1
            },
            new ResourceAppointment {
                Caption = ""Launch New Website"",
                StartDate = date + (new TimeSpan(1, 12, 20, 0)),
                EndDate = date + (new TimeSpan(1, 14, 0, 0)),
                Status = 1,
                ResourceId = 2
            },
            new ResourceAppointment {
                Caption = ""Upgrade Server Hardware"",
                StartDate = date + (new TimeSpan(1, 9, 0, 0)),
                EndDate = date + (new TimeSpan(1, 12, 0, 0)),
                Status = 1,
                ResourceId = 2
            },
            new ResourceAppointment {
                Caption = ""Book Flights to San Fran for Sales Trip"",
                StartDate = date + (new TimeSpan(0, 14, 0, 0)),
                EndDate = date + (new TimeSpan(0, 17, 0, 0)),
                Status = 1,
                ResourceId = 3
            },
            new ResourceAppointment {
                Caption = ""Approve New Online Marketing Strategy"",
                StartDate = date + (new TimeSpan(0,  12, 0, 0)),
                EndDate = date + (new TimeSpan(0, 15, 0, 0)),
                Status = 1,
                ResourceId = 4
            }
        };
        return dataSource;
    }

    public static List<Resource> GetResources() {
        return new List<Resource>() {
            new Resource() { Id=0 , Name=""John Heart"", GroupId=100, BackgroundCss=""dx-green-color"", TextCss=""text-white"" },
            new Resource() { Id=1 , Name=""Samantha Bright"", GroupId=101, BackgroundCss=""dx-orange-color"", TextCss=""text-white"" },
            new Resource() { Id=2 , Name=""Arthur Miller"", GroupId=100, BackgroundCss=""dx-purple-color"", TextCss=""text-white"" },
            new Resource() { Id=3 , Name=""Robert Reagan"", GroupId=101, BackgroundCss=""dx-indigo-color"", TextCss=""text-white"" },
            new Resource() { Id=4 , Name=""Greta Sims"", GroupId=100, BackgroundCss=""dx-red-color"", TextCss=""text-white"" }
        };
    }

    public static List<Resource> GetResourceGroups() {
        return new List<Resource>() {
            new Resource() { Id=100, Name=""Sales and Marketing"", IsGroup=true },
            new Resource() { Id=101, Name=""Engineering"", IsGroup=true }
        };
    }
}")
</CodeSnippetTabPage>
</CodeSnippetTabbed>
